function [HJOLIV,HJPLAG,HJCPX,HJQTZ,QHJQTZ,QHJOLIV,QHJCPX,PHJPLAG,...
    PHJOLIV,PHJCPX,MAL2O3,MCAO,MMGO,HJAL2O3,HJCAO,HJMGO] = OPAM (DAT,P)

% Subroutine to be used in conjunction with Yang et al. 1996.
% This code has been adapted from FORTRAN by PM Gregg, 2007.
%
% SUBROUTINE OPAM (DAT,P,HJOLIV,HJPLAG,HJCPX,HJQTZ,QHJQTZ,QHJOLIV,
%                  QHJCPX,PHJPLAG,PHJOLIV,PHJCPX,MAL2O3,MCAO,MMGO,
%                  HJAL2O3,HJCAO,HJMGO)

% DIMENSION DAT(51,12)
% REAL DAT(51,12),P
% REAL HJQTZ,HJOLIV,HJPLAG,HJCPX,HJAL2O3,HJCAO,HJMGO
% REAL QHJQTZ,QHJOLIV,QHJCPX,PHJPLAG,PHJOLIV,PHJCPX
% REAL MSIO2,MTIO2,MAL2O3,MFE2O3,MFEO,MMGO,MCAO,MNA2O
% REAL MK2O,MP2O5,MCR2O3,MTOTAL,OX,AP,OR
% REAL SUM,QTZ,OLIV,CPX,PLAG,QQTZ,QOLIV,QCPX
% REAL PPLAG,POLIV,PCPX
% clear P
% P = 0.001;

MSIO2=DAT(1,1)/60.09;
MTIO2=DAT(1,2)/79.9;
MAL2O3=DAT(1,3)/102*2;
MFE2O3=DAT(1,4)/159.7*2;
MFEO=DAT(1,5)/71.85;
MMGO=DAT(1,6)/40.3;
MCAO=DAT(1,7)/56.08;
MNA2O=DAT(1,8)/62*2;
MK2O=DAT(1,9)/94.2*2;
MP2O5=DAT(1,10)/141.9*2;
MCR2O3=DAT(1,11)/152*2;
MTOTAL=MSIO2+MTIO2+MAL2O3+MFE2O3+MFEO+MMGO+MCAO+MNA2O+MK2O+MP2O5+MCR2O3;
MSIO2=MSIO2/MTOTAL;
MTIO2=MTIO2/MTOTAL;
MAL2O3=MAL2O3/MTOTAL;
MFE2O3=MFE2O3/MTOTAL;
MFEO=MFEO/MTOTAL;
MMGO=MMGO/MTOTAL;
MCAO=MCAO/MTOTAL;
MNA2O=MNA2O/MTOTAL;
MK2O=MK2O/MTOTAL;
MP2O5=MP2O5/MTOTAL;
%MCR2O3=CR2O3/MTOTAL;  < -- this line didn't work.  :P changed to next line
MCR2O3=MCR2O3/MTOTAL;
HJAL2O3=0.236435+0.002182*P+0.109199*MNA2O+0.593295*MK2O-...
    0.349718*MTIO2-0.299419*MFEO-0.130353*MSIO2;
HJMGO=-0.276789+0.001139*P-0.542727*MNA2O-0.947294*MK2O-0.117029*MTIO2...
	-0.489758*MFEO+2.085584*MSIO2-2.400115*MSIO2^2;
HJCAO=1.132807-0.003388*P-0.569004*MNA2O-0.776108*MK2O-0.672285*MTIO2...
	-0.213801*MFEO-3.355327*MSIO2+2.830219*MSIO2^2;
SUM=MSIO2-HJCAO-(2*(MNA2O+MK2O))+MTIO2+(2*MP2O5)+MFE2O3;
QTZ=(MSIO2-(HJMGO+MFEO)/2-(1.5*HJCAO)-(HJAL2O3/4));
QTZ=(QTZ-(2.75*(MNA2O+MK2O)));
QTZ=(QTZ+(MCR2O3/2)+(MTIO2/2)+(2.5*MP2O5))/SUM;
PLAG=(HJAL2O3+MNA2O-MK2O)/(2*SUM);
OLIV=((MFEO+HJMGO-MTIO2-HJCAO)/2+(HJAL2O3-MNA2O-MK2O-MCR2O3)/...
    4+(0.83333*MP2O5))/SUM;
CPX=(HJCAO-(HJAL2O3/2)+((MK2O+MNA2O)/2)-(1.66667*MP2O5))/SUM;
OX=((MCR2O3/2)+MTIO2+MFE2O3)/SUM;
AP=MP2O5/(3*SUM);
OR=MK2O/SUM;
HJQTZ=QTZ;
HJPLAG=4*PLAG;
HJOLIV=2*OLIV;
HJCPX=3*CPX;
QHJQTZ=100*HJQTZ/(HJQTZ+HJOLIV+HJCPX);
QHJOLIV=100*HJOLIV/(HJQTZ+HJOLIV+HJCPX);
QHJCPX=100*HJCPX/(HJQTZ+HJOLIV+HJCPX);
PHJPLAG=100*HJPLAG/(HJPLAG+HJOLIV+HJCPX);
PHJOLIV=100*HJOLIV/(HJPLAG+HJOLIV+HJCPX);
PHJCPX=100*HJCPX/(HJPLAG+HJOLIV+HJCPX);
% C	WRITE (9,*) P,MNA2O,MK2O,MTIO2,MFEO,MSIO2
% C	WRITE (9,*) MCAO,MAL2O3,MMGO
% C	WRITE (9,*) SUM,HJOLIV,HJPLAG,HJCPX,HJQTZ
% C	PAUSE
